import http from "@/data-services/http";
import appEnvironment from "@/app-environments";
import {
  T_Camera_List_Request,
  T_Camera_Events_Request,
  T_GetDevicesBind_Request,
  T_GetDevicesVideo_Request,
  T_ConfirmedCamera_Request,
  T_Camera_Statistics_Request,
  T_GetRoiBind_Request,
} from "./types.ts";

//获取环境变量
const BASE_URL_DEV = import.meta.env.VITE_BASE_URL_DEV;
const BASE_URL_ONLINE = import.meta.env.VITE_BASE_URL_ONLINE;

const uriBase = "/api/devices";
const {
  networkHosts: {
    apiServer1: { fullHost },
  },
} = appEnvironment;
let url_pro = new URL(fullHost);
const currentUrl = new URL(window.location.href); //获取浏览器输入URL地址

//判断是否为域名地址
const isDomain = !/^\d+\.\d+\.\d+\.\d+$/.test(currentUrl.hostname);

//根据当前环境选择合适的 URL（线上模式的话根据是否是域名地址来选择,8090端口为后端暴露端口号）
const BASE_URL =
  process.env.NODE_ENV === "production"
    ? isDomain
      ? BASE_URL_ONLINE
      : `${url_pro.protocol}//${url_pro.hostname}:8090`
    : BASE_URL_DEV;

function GetEquipments_CameraList(data: T_Camera_List_Request) {
  type _T_XmeDSI_Xhr_ResponseData = T_ResponseDataOfSuccess;

  type _T_XmeDSI_Xhr_TransformedFullResponse = _T_XmeDSI_Xhr_ResponseData;

  return http<
    _T_XmeDSI_Xhr_ResponseData,
    _T_XmeDSI_Xhr_TransformedFullResponse,
    T_Camera_List_Request
  >({
    method: "post",
    url: uriBase,
    data: data,
  });
}

function GetEvents_CameraList(data: T_Camera_Events_Request) {
  type _T_XmeDSI_Xhr_ResponseData = T_ResponseDataOfSuccess;

  type _T_XmeDSI_Xhr_TransformedFullResponse = _T_XmeDSI_Xhr_ResponseData;

  return http<
    _T_XmeDSI_Xhr_ResponseData,
    _T_XmeDSI_Xhr_TransformedFullResponse,
    T_Camera_Events_Request
  >({
    method: "post",
    url: uriBase,
    data: data,
  });
}

function getSingleCamera_RtspFullUri(id: string): Promise<Array<string>> {
  type _T_ResponseData = T_ResponseDataOfSuccess<Array<string>>;

  type _T_TransformedFullResponse = _T_ResponseData;

  type _T_DI_FinallyProvidedData = Array<string>;

  type _T_RequestPayload = {
    _id: string;
  };

  const url = "/api/stream/get-by-device";

  const data: _T_RequestPayload = {
    _id: id,
  };

  return http<_T_ResponseData, _T_TransformedFullResponse, _T_RequestPayload>({
    method: "post",
    url,
    data,
  }).then<_T_DI_FinallyProvidedData>((responseData) => {
    const serverSideRootRecordList = responseData.data;
    return serverSideRootRecordList;
  });
}

function xmeDSIC_Solutions_GetDevicesBind(data: T_GetDevicesBind_Request) {
  type _T_XmeDSI_Xhr_ResponseData = T_ResponseDataOfSuccess;

  type _T_XmeDSI_Xhr_TransformedFullResponse = _T_XmeDSI_Xhr_ResponseData;

  return http<
    _T_XmeDSI_Xhr_ResponseData,
    _T_XmeDSI_Xhr_TransformedFullResponse,
    T_GetDevicesBind_Request
  >({
    method: "post",
    url: "/api/infer-pipeline",
    data: data,
  });
}

function GetDevicesVideoList(data: T_GetDevicesVideo_Request) {
  type _T_XmeDSI_Xhr_ResponseData = T_ResponseDataOfSuccess;

  type _T_XmeDSI_Xhr_TransformedFullResponse = _T_XmeDSI_Xhr_ResponseData;

  return http<
    _T_XmeDSI_Xhr_ResponseData,
    _T_XmeDSI_Xhr_TransformedFullResponse,
    T_GetDevicesVideo_Request
  >({
    method: "post",
    url: "/api/shizhi/v1",
    data: data,
  });
}

function confirmedCamera(data: T_ConfirmedCamera_Request) {
  type _T_XmeDSI_Xhr_ResponseData = T_ResponseDataOfSuccess;

  type _T_XmeDSI_Xhr_TransformedFullResponse = _T_XmeDSI_Xhr_ResponseData;

  return http<
    _T_XmeDSI_Xhr_ResponseData,
    _T_XmeDSI_Xhr_TransformedFullResponse,
    T_ConfirmedCamera_Request
  >({
    method: "post",
    url: "/api/shizhi/v1/confirm",
    data: data,
  });
}

function resetCamera(data: T_ConfirmedCamera_Request) {
  type _T_XmeDSI_Xhr_ResponseData = T_ResponseDataOfSuccess;

  type _T_XmeDSI_Xhr_TransformedFullResponse = _T_XmeDSI_Xhr_ResponseData;

  return http<
    _T_XmeDSI_Xhr_ResponseData,
    _T_XmeDSI_Xhr_TransformedFullResponse,
    T_ConfirmedCamera_Request
  >({
    method: "post",
    url: "/api/shizhi/v1/reset",
    data: data,
  });
}

function warnStatistics(data: T_Camera_Statistics_Request) {
  type _T_XmeDSI_Xhr_ResponseData = T_ResponseDataOfSuccess;

  type _T_XmeDSI_Xhr_TransformedFullResponse = _T_XmeDSI_Xhr_ResponseData;

  return http<
    _T_XmeDSI_Xhr_ResponseData,
    _T_XmeDSI_Xhr_TransformedFullResponse,
    T_Camera_Statistics_Request
  >({
    method: "post",
    url: "/api/shizhi/v1/statistics",
    data: data,
  });
}

function login(data: T_LoginRequest) {
  type _T_ResponseData = T_ResponseDataOfSuccess;

  return http<_T_ResponseData, _T_ResponseData, T_LoginRequest>({
    url: `/auth/login`,
    method: "post",
    data,
  });
}
function downloadExcel(data: T_GetDevicesVideo_Request) {
  type _T_ResponseData = T_ResponseDataOfSuccess;

  return http<_T_ResponseData, _T_ResponseData, T_GetDevicesVideo_Request>({
    url: `/api/shizhi/v1/download_excel`,
    method: "post",
    data,
  });
}

function xmeDSIC_Solutions_GetRoiCameraBind(data: T_GetRoiBind_Request) {
  type _T_XmeDSI_Xhr_ResponseData = T_ResponseDataOfSuccess;

  type _T_XmeDSI_Xhr_TransformedFullResponse = _T_XmeDSI_Xhr_ResponseData;

  return http<
    _T_XmeDSI_Xhr_ResponseData,
    _T_XmeDSI_Xhr_TransformedFullResponse,
    T_GetRoiBind_Request
  >({
    method: "post",
    url: "api/proj/config-get",
    data: data,
  });
}

//事件信息
function xmeDSIC_Solutions_GetAction(data) {
  return http({
    method: "post",
    headers: {
      "Content-Type": "application/json",
    },
    url: `${BASE_URL}/event/page`,
    data: JSON.stringify(data),
  });
}

//操作历史查询
function xmeDSIC_Solutions_History(data, pageSize, pageNum) {
  return http({
    method: "post",
    headers: {
      "Content-Type": "application/json",
    },
    url: `${BASE_URL}/LX/api/Anomalous?pageSize=${pageSize}&pageNum=${pageNum}`,
    data: JSON.stringify(data),
  });
}
//复位
function xmeDSIC_Solutions_reset(data) {
  return http({
    method: "post",
    headers: {
      "Content-Type": "application/json",
    },
    url: `${BASE_URL}/LX/api/reset`,
    data: JSON.stringify(data),
  });
}

export {
  login,
  GetEvents_CameraList,
  GetEquipments_CameraList,
  getSingleCamera_RtspFullUri,
  xmeDSIC_Solutions_GetDevicesBind,
  GetDevicesVideoList,
  confirmedCamera,
  warnStatistics,
  resetCamera,
  downloadExcel,
  xmeDSIC_Solutions_GetRoiCameraBind,
  xmeDSIC_Solutions_GetAction,
  xmeDSIC_Solutions_History,
  xmeDSIC_Solutions_reset,
};
